﻿using Microsoft.AspNetCore.Mvc;
using SD.Infrastructure.DTOBase;
using SD.Toolkits.AspNetCore.Attributes;
using SlamDunk.MES.IAppService.DTOs.Inputs;
using SlamDunk.MES.IAppService.DTOs.Outputs.ConfigurationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.DefinitionContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.AppService.Host.Controllers
{
    /// <summary>
    /// 配置管理服务WebApi接口
    /// </summary>
    [ApiController]
    [Route("Api/[controller]/[action]")]
    public class ConfigurationController : ControllerBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 配置管理服务契约接口
        /// </summary>
        private readonly IConfigurationContract _configurationContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ConfigurationController(IConfigurationContract configurationContract)
        {
            this._configurationContract = configurationContract;
        }

        #endregion


        //命令部分

        #region # 保存用户扩展 —— void SaveUserExtension(string loginId, Guid personId...
        /// <summary>
        /// 保存用户扩展
        /// </summary>
        /// <param name="loginId">用户账号</param>
        /// <param name="personId">人员Id</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="terminalIds">终端Id集</param>
        /// <param name="processSegmentIds">工段Id集</param>
        /// <param name="relatedStorageLocationParams">用户相关储存位置参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveUserExtension(string loginId, Guid personId, Guid facilityId, [FromQuery] IEnumerable<Guid> terminalIds, [FromQuery] IEnumerable<Guid> processSegmentIds, [FromQuery] IEnumerable<UserRelatedStorageLocationParam> relatedStorageLocationParams)
        {
            this._configurationContract.SaveUserExtension(loginId, personId, facilityId, terminalIds, processSegmentIds, relatedStorageLocationParams);
        }
        #endregion

        #region # 创建终端 —— void CreateTerminal(string terminalNo, string terminalName...
        /// <summary>
        /// 创建终端
        /// </summary>
        /// <param name="terminalNo">终端编号</param>
        /// <param name="terminalName">终端名称</param>
        /// <param name="description">描述</param>
        [HttpPost]
        [WrapPostParameters]
        public void CreateTerminal(string terminalNo, string terminalName, string description)
        {
            this._configurationContract.CreateTerminal(terminalNo, terminalName, description);
        }
        #endregion

        #region # 修改终端 —— void UpdateTerminal(Guid terminalId, string terminalNo...
        /// <summary>
        /// 修改终端
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        /// <param name="terminalNo">终端编号</param>
        /// <param name="terminalName">终端名称</param>
        /// <param name="description">描述</param>
        [HttpPost]
        [WrapPostParameters]
        public void UpdateTerminal(Guid terminalId, string terminalNo, string terminalName, string description)
        {
            this._configurationContract.UpdateTerminal(terminalId, terminalNo, terminalName, description);
        }
        #endregion

        #region # 删除终端 —— void RemoveTerminal(Guid terminalId)
        /// <summary>
        /// 删除终端
        /// </summary>
        /// <param name="terminalId">终端Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void RemoveTerminal(Guid terminalId)
        {
            this._configurationContract.RemoveTerminal(terminalId);
        }
        #endregion

        #region # 创建硬件 —— void CreateHardware(string hardwareNo, string hardwareName...
        /// <summary>
        /// 创建硬件
        /// </summary>
        /// <param name="hardwareNo">硬件编号</param>
        /// <param name="hardwareName">硬件名称</param>
        /// <param name="hardwareType">硬件类型</param>
        /// <param name="model">型号</param>
        /// <param name="description">描述</param>
        [HttpPost]
        [WrapPostParameters]
        public void CreateHardware(string hardwareNo, string hardwareName, HardwareType hardwareType, string model, string description)
        {
            this._configurationContract.CreateHardware(hardwareNo, hardwareName, hardwareType, model, description);
        }
        #endregion

        #region # 修改硬件 —— void UpdateHardware(Guid hardwareId, string hardwareNo...
        /// <summary>
        /// 修改硬件
        /// </summary>
        /// <param name="hardwareId">硬件Id</param>
        /// <param name="hardwareNo">硬件编号</param>
        /// <param name="hardwareName">硬件名称</param>
        /// <param name="model">型号</param>
        /// <param name="description">描述</param>
        [HttpPost]
        [WrapPostParameters]
        public void UpdateHardware(Guid hardwareId, string hardwareNo, string hardwareName, string model, string description)
        {
            this._configurationContract.UpdateHardware(hardwareId, hardwareNo, hardwareName, model, description);
        }
        #endregion

        #region # 删除硬件 —— void RemoveHardware(Guid hardwareId)
        /// <summary>
        /// 删除硬件
        /// </summary>
        /// <param name="hardwareId">硬件Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void RemoveHardware(Guid hardwareId)
        {
            this._configurationContract.RemoveHardware(hardwareId);
        }
        #endregion

        #region # 配置硬件网口通信 —— void ConfigureHardwareNetworkMode(Guid hardwareId...
        /// <summary>
        /// 配置硬件网口通信
        /// </summary>
        /// <param name="hardwareId">硬件Id</param>
        /// <param name="terminalId">终端Id</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="port">端口号</param>
        [HttpPost]
        [WrapPostParameters]
        public void ConfigureHardwareNetworkMode(Guid hardwareId, Guid? terminalId, string ipAddress, int port)
        {
            this._configurationContract.ConfigureHardwareNetworkMode(hardwareId, terminalId, ipAddress, port);
        }
        #endregion

        #region # 配置硬件串口通信 —— void ConfigureHardwareSerialPortMode(Guid hardwareId...
        /// <summary>
        /// 配置硬件串口通信
        /// </summary>
        /// <param name="hardwareId">硬件Id</param>
        /// <param name="terminalId">终端Id</param>
        /// <param name="comPort">串口端口号</param>
        /// <param name="baudRate">波特率</param>
        /// <param name="stopBit">停止位</param>
        /// <param name="parityBit">校验位</param>
        /// <param name="dataBit">数据位</param>
        /// <param name="dtr">数据终端准备</param>
        /// <param name="rts">请求发送</param>
        [HttpPost]
        [WrapPostParameters]
        public void ConfigureHardwareSerialPortMode(Guid hardwareId, Guid? terminalId, string comPort, int baudRate, StopBits stopBit, Parity parityBit, int dataBit, bool dtr, bool rts)
        {
            this._configurationContract.ConfigureHardwareSerialPortMode(hardwareId, terminalId, comPort, baudRate, stopBit, parityBit, dataBit, dtr, rts);
        }
        #endregion

        #region # 配置硬件USB口通信 —— void ConfigureUsbMode(Guid hardwareId, Guid? terminalId)
        /// <summary>
        /// 配置硬件USB口通信
        /// </summary>
        /// <param name="hardwareId">硬件Id</param>
        /// <param name="terminalId">终端Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void ConfigureHardwareUsbMode(Guid hardwareId, Guid? terminalId)
        {
            this._configurationContract.ConfigureHardwareUsbMode(hardwareId, terminalId);
        }
        #endregion

        #region # 创建工作通知 —— void CreateNotification(string title, string content...
        /// <summary>
        /// 创建工作通知
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="issuedDate">下发日期</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="priority">优先级</param>
        /// <param name="description">描述</param>
        [HttpPost]
        [WrapPostParameters]
        public void CreateNotification(string title, string content, DateTime issuedDate, Guid? facilityId, int priority, string description)
        {
            this._configurationContract.CreateNotification(title, content, issuedDate, facilityId, priority, description);
        }
        #endregion

        #region # 删除工作通知 —— void RemoveNotification(Guid notificationId)
        /// <summary>
        /// 删除工作通知
        /// </summary>
        /// <param name="notificationId">工作通知Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void RemoveNotification(Guid notificationId)
        {
            this._configurationContract.RemoveNotification(notificationId);
        }
        #endregion


        //查询部分

        #region # 分页获取用户扩展列表 —— PageModel<UserExtensionInfo> GetUserExtensionsByPage(Guid? facilityId...
        /// <summary>
        /// 分页获取用户扩展列表
        /// </summary>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>用户扩展列表</returns>
        [HttpGet]
        public PageModel<UserExtensionInfo> GetUserExtensionsByPage(Guid? facilityId, int pageIndex, int pageSize)
        {
            return this._configurationContract.GetUserExtensionsByPage(facilityId, pageIndex, pageSize);
        }
        #endregion

        #region # 获取用户扩展 —— UserExtensionInfo GetUserExtension(string loginId)
        /// <summary>
        /// 获取用户扩展
        /// </summary>
        /// <param name="loginId">用户账号</param>
        /// <returns>用户扩展信息</returns>
        [HttpGet]
        public UserExtensionInfo GetUserExtension(string loginId)
        {
            return this._configurationContract.GetUserExtension(loginId);
        }
        #endregion

        #region # 获取用户相关终端列表 —— IEnumerable<TerminalInfo> GetUserRelatedTerminals(string loginId)
        /// <summary>
        /// 获取用户相关终端列表
        /// </summary>
        /// <param name="loginId">用户账号</param>
        /// <returns>用户相关终端列表</returns>
        [HttpGet]
        public IEnumerable<TerminalInfo> GetUserRelatedTerminals(string loginId)
        {
            return this._configurationContract.GetUserRelatedTerminals(loginId);
        }
        #endregion

        #region # 获取用户相关工艺段列表 —— {ProcessInfo, ProcessSegmentInfo[]} GetUserRelatedProcessSegments(...
        /// <summary>
        /// 获取用户相关工艺段列表
        /// </summary>
        /// <param name="loginId">用户账号</param>
        /// <returns>用户相关工艺段列表</returns>
        [HttpGet]
        public IDictionary<ProcessInfo, ProcessSegmentInfo[]> GetUserRelatedProcessSegments(string loginId)
        {
            return this._configurationContract.GetUserRelatedProcessSegments(loginId);
        }
        #endregion

        #region # 获取用户相关储存位置列表 —— IEnumerable<UserRelatedStorageLocationInfo> GetUserRelatedStorageLocations(...
        /// <summary>
        /// 获取用户相关储存位置列表
        /// </summary>
        /// <param name="loginId">用户账号</param>
        /// <returns>用户相关储存位置列表</returns>
        [HttpGet]
        public IEnumerable<UserRelatedStorageLocationInfo> GetUserRelatedStorageLocations(string loginId)
        {
            return this._configurationContract.GetUserRelatedStorageLocations(loginId);
        }
        #endregion

        #region # 分页获取终端列表 —— PageModel<TerminalInfo> GetTerminalsByPage(string keywords...
        /// <summary>
        /// 分页获取终端列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>终端列表</returns>
        [HttpGet]
        public PageModel<TerminalInfo> GetTerminalsByPage(string keywords, int pageIndex, int pageSize)
        {
            return this._configurationContract.GetTerminalsByPage(keywords, pageIndex, pageSize);
        }
        #endregion

        #region # 获取终端 —— TerminalInfo GetTerminal(string terminalNo)
        /// <summary>
        /// 获取终端
        /// </summary>
        /// <param name="terminalNo">终端编号</param>
        /// <returns>终端</returns>
        [HttpGet]
        public TerminalInfo GetTerminal(string terminalNo)
        {
            return this._configurationContract.GetTerminal(terminalNo);
        }
        #endregion

        #region # 分页获取硬件列表 —— PageModel<HardwareInfo> GetHardwaresByPage(string keywords...
        /// <summary>
        /// 分页获取硬件列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="hardwareType">硬件类型</param>
        /// <param name="terminalId">终端Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>硬件列表</returns>
        [HttpGet]
        public PageModel<HardwareInfo> GetHardwaresByPage(string keywords, HardwareType? hardwareType, Guid? terminalId, int pageIndex, int pageSize)
        {
            return this._configurationContract.GetHardwaresByPage(keywords, hardwareType, terminalId, pageIndex, pageSize);
        }
        #endregion

        #region # 分页获取工作通知列表 —— PageModel<NotificationInfo> GetNotificationsByPage(string keywords...
        /// <summary>
        /// 分页获取工作通知列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>工作通知列表</returns>
        [HttpGet]
        public PageModel<NotificationInfo> GetNotificationsByPage(string keywords, Guid? facilityId, DateTime? startTime, DateTime? endTime, int pageIndex, int pageSize)
        {
            return this._configurationContract.GetNotificationsByPage(keywords, facilityId, startTime, endTime, pageIndex, pageSize);
        }
        #endregion
    }
}
